package com.becca.cake.service.category.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.core.exception.ServiceException;
import com.becca.cake.dao.category.GoodsCategoryDAO;
import com.becca.cake.model.category.GoodCategoryModel;
import com.becca.cake.model.system.AdminDO;
import com.becca.cake.service.category.IGoodCategoryService;

/**
 * 分类管理service实现类
 * @copyright  尊星(上海)信息科技有限公司
 * @author mouwanming
 *
 */
@Service
public class GoodCategoryServiceImpl implements IGoodCategoryService
{
	@Autowired
	GoodsCategoryDAO goodsCategoryDAO;
	
	@Override
	public List<GoodCategoryModel> selectGoodCategoryName() throws Exception
	{
		return this.goodsCategoryDAO.selectGoodCategoryName();
	}

	@Override
	public List<GoodCategoryModel> selectGoodCategory(Map<String, Object> map)
			throws Exception
	{
		return this.goodsCategoryDAO.selectGoodCategory(map);
	}

	@Override
	public Long selectGoodCategoryCount(Map<String, Object> map)
			throws Exception
	{
		return this.goodsCategoryDAO.selectGoodCategoryCount(map);
	}

	@Override
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void insertOrUpdateGoodCategory(GoodCategoryModel goodCategoryModel,AdminDO adminDo) throws Exception
	{
			if(goodCategoryModel.getId() == null){//添加
				goodCategoryModel.setCreatedName(adminDo.getTrueName());
				goodCategoryModel.setCategoryStatus(GoodCategoryModel.CATEGORY_STATUS_NOMAL);
				this.goodsCategoryDAO.insertGoodCategory(goodCategoryModel);
			}else{//修改
				goodCategoryModel.setUpdatedName(adminDo.getTrueName());
				goodCategoryModel.setCategoryStatus(GoodCategoryModel.CATEGORY_STATUS_NOMAL);
				this.goodsCategoryDAO.updateGoodCategory(goodCategoryModel);
			}
	}

	@Override
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void updateGoodCategoryById(Map<String, Object> map) throws Exception
	{
		 Integer id = Integer.valueOf(map.get("id").toString());
		 String categoryStatus = String.valueOf(map.get("categoryStatus"));
		 GoodCategoryModel.validCgStatus(id, categoryStatus);
		 GoodCategoryModel gc = this.selectGoodCategoryById(id);
		 String gcNowStatus = gc.getCategoryStatus().toString();
		 
		 if(categoryStatus.equals(gcNowStatus) && GoodCategoryModel.CATEGORY_STATUS_FORBIDDEN.toString().equals(categoryStatus)){
			 throw new ServiceException(GoodCategoryModel.CATEGORY_STATUS_FORBIDDEN_EXIST);
		 }else if(categoryStatus.equals(gcNowStatus) && GoodCategoryModel.CATEGORY_STATUS_NOMAL.toString().equals(categoryStatus)){
			 throw new ServiceException(GoodCategoryModel.CATEGORY_STATUS_NOMAL_EXIST);
		 }
		 this.goodsCategoryDAO.updateGoodCategoryById(map);
	}

	@Override
	public GoodCategoryModel selectGoodCategoryById(Integer id)
			throws Exception
	{
		return this.goodsCategoryDAO.selectGoodCategoryById(id);
	}

}
